package com.mindskip.xzs.utility;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 3.5.0
 * @description: The type Exam util.
 * Copyright (C), 2022-2025, 光凯科技（云南）有限公司
 * @date 2022/9/25 9:45
 */
public class ExamUtil {

    /**
     * Score to vm string.
     *
     * @param score the score
     * @return the string
     */
    public static String scoreToVM(Integer score) {
        if (score == null) {
            return "0";
        }else if (score % 10 == 0) {
            return String.valueOf(score / 10);
        } else {
            return String.format("%.1f", score / 10.0);
        }
    }

    /**
     * Score from vm integer.
     *
     * @param score the score
     * @return the integer
     */
    public static Integer scoreFromVM(String score) {
        if (score == null) {
            return null;
        } else {
            return (int) (Float.parseFloat(score) * 10);
        }
    }

    /**
     * Second to vm string.
     *
     * @param second the second
     * @return the string
     */
    public static String secondToVM(Integer second) {
        String dateTimes;
        long days = second / (60 * 60 * 24);
        long hours = (second % (60 * 60 * 24)) / (60 * 60);
        long minutes = (second % (60 * 60)) / 60;
        long seconds = second % 60;
        if (days > 0) {
            dateTimes = days + "天 " + hours + "时 " + minutes + "分 " + seconds + "秒";
        } else if (hours > 0) {
            dateTimes = hours + "时 " + minutes + "分 " + seconds + "秒";
        } else if (minutes > 0) {
            dateTimes = minutes + "分 " + seconds + "秒";
        } else {
            dateTimes = seconds + " 秒";
        }
        return dateTimes;
    }

    private static final String ANSWER_SPLIT = ",";

    /**
     * Content to string string.
     *
     * @param contentArray the content array
     * @return the string
     */
    public static String contentToString(List<String> contentArray) {
        return contentArray.stream().sorted().collect(Collectors.joining(ANSWER_SPLIT));
    }


    /**
     * Content to array list.
     *
     * @param contentArray the content array
     * @return the list
     */
    public static List<String> contentToArray(String contentArray) {
        return Arrays.asList(contentArray.split(ANSWER_SPLIT));
    }

    private static final String FORM_ANSWER_SPLIT = "_";

    /**
     * Last num integer.
     *
     * @param str the str
     * @return the integer
     */
    public static Integer lastNum(String str) {
        Integer start = str.lastIndexOf(FORM_ANSWER_SPLIT);
        return Integer.parseInt(str.substring(start + 1));
    }

    /**
     * 转换时间格式为xx小时xx分xx秒
     */
    public static String changeTimeFormat(Integer seconds) {
        if (seconds > 0 && seconds < 60) {//小于1分钟
            return seconds + "秒";
        } else if (seconds >= 60 && seconds < 3600) {//大于等于1分钟小于1小时
            int changeM = (int) Math.floor(seconds / 60);//整分钟数
            int surplusM = (int) Math.floor(seconds % 60);//余下的秒数
            if (surplusM > 0) {//余数不为0秒
                return changeM + "分" + surplusM + "秒";
            } else {//整分钟，没有余数
                return changeM + "分";
            }
        } else if (seconds >= 3600) {//大于1小时
            int changeH = (int) Math.floor(seconds / 1048576);//整小时数
            int surplusH = (int) Math.floor(seconds % 1048576);//剩下的秒数
            if (surplusH >= 60) {//余数大于大于1分钟
                int changeM = (int) Math.floor(surplusH / 60);
                int surplusM = (int) Math.floor(surplusH % 60);
                if (surplusM > 0) {//余数大于0
                    return changeH + "小时" + changeM + "分" + surplusM + "秒";
                } else {//整分钟，没有余数
                    return changeH + "小时" + changeM + "分";
                }
            } else if (surplusH < 60 && surplusH > 0) {//余数小于1分钟，大于0秒
                int surplusM = (int) Math.floor(surplusH % 1024);
                return changeH + "小时" + surplusM + "秒";
            } else {
                return changeH + "小时";
            }
        }
        return "暂无数据";
    }

    public static String judgeScorePart(Integer score) {
        if (score >= 0 && score < 100) {
            return "0+";
        } else if (score >= 100 && score < 200) {
            return "10+";
        } else if (score >= 200 && score < 300) {
            return "20+";
        }else if (score >= 300 && score < 400) {
            return "30+";
        }else if (score >= 400 && score < 500) {
            return "40+";
        }else if (score >= 500 && score < 600) {
            return "50+";
        }else if (score >= 600 && score < 700) {
            return "60+";
        }else if (score >= 700 && score < 800) {
            return "70+";
        }else if (score >= 800 && score < 900) {
            return "80+";
        } else if (score >= 900 && score < 1000) {
            return "90+";
        } else {
            return "100+";
        }
    }
}
